﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Toolbox Title Control Base Class
 *      Provides a base definition for toolbox controls which show a title.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Adaptive.Foundation.Windows.Forms.Properties;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Provides a base definition for toolbox controls which show a title.
    /// </summary>
    [ToolboxItem(false)]
    public partial class ToolboxCollapsiblePanelTitle : TooboxTitleControlBase
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        /// <summary>
        /// Occurs when the panel is collapsed.
        /// </summary>
        public event EventHandler Collapsed;
        /// <summary>
        /// Occurs when the panel is expanded.
        /// </summary>
        public event EventHandler Expanded;
        /// <summary>
        /// Occurs when the viewable state of the panel changes.
        /// </summary>
        public event EventHandler ConditionChanged;
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// The collapsed or expanded state of the control.
        /// </summary>
        private CollapsiblePanelState _state = CollapsiblePanelState.Collapsed;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ToolboxCollapsiblePanelTitle()
            : base()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ContainerControl, false);
            SetStyle(ControlStyles.Opaque, false);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Selectable, false);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);

            //Set defaults.
            BackColor = SystemColors.Control;
            ForeColor = SystemColors.ControlText;
            MinimumSize = new Size(1000, 16);
            MaximumSize = new Size(100, 16);

        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }         
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the state of the visual.
        /// </summary>
        /// <value>The state of the visual.</value>
        public CollapsiblePanelState VisualState
        {
            get { return _state; }
            set
            {
                if (value != _state)
                {
                    _state = value;
                    SetImage();
                    if (value == CollapsiblePanelState.Expanded)
                        OnExpanded(EventArgs.Empty);
                    else
                        OnCollapsed(EventArgs.Empty);

                    OnConditionChanged(EventArgs.Empty);
                    Invalidate();
                }
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the items contained within the control.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            ClickImage.Click +=new EventHandler(HandlePlusMinusClick);
        }
        /// <summary>
        /// Removes the event handlers for the items contained within the control.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            ClickImage.Click -= new EventHandler(HandlePlusMinusClick);
        }
        /// <summary>
        /// Initializes the content of the control before the event handlers are assigned.
        /// </summary>
        protected override void InitializeContent()
        {
            SetImage();
        }
        /// <summary>
        /// Raises the <see cref="E:ConditionChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnConditionChanged(EventArgs e)
        {
            if (ConditionChanged != null)
                ConditionChanged(this, e);
        }
        /// <summary>
        /// Raises the <see cref="E:Collapsed"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnCollapsed(EventArgs e)
        {
            if (Collapsed != null)
                Collapsed(this, e);
        }
        /// <summary>
        /// Raises the <see cref="E:Expanded"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnExpanded(EventArgs e)
        {
            if (Expanded != null)
                Expanded(this, e);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        /// <summary>
        /// Handles the event when the plus or minus image is clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing 
        /// the event data.</param>
        private void HandlePlusMinusClick(object sender, EventArgs e)
        {
            if (_state == CollapsiblePanelState.Collapsed)
            {
                _state = CollapsiblePanelState.Expanded;
                SetImage();
                OnExpanded(e);
                OnConditionChanged(e);
            }
            else
            {
                _state = CollapsiblePanelState.Collapsed;
                SetImage();
                OnCollapsed(e);
                OnConditionChanged(e);
            }                   
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        /// <summary>
        /// Sets the image display.
        /// </summary>
        private void SetImage()
        {
            if (_state == CollapsiblePanelState.Collapsed)
                ClickImage.Image = Resources.Plus;
            else
                ClickImage.Image = Resources.Minus;
        }
        #endregion

    }
}
